<?php

namespace App\Http\Controllers\Admin;

use App\Models\Article;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ArticleController extends Controller
{
    //文章列表 显示
    public function index()
    {
        $data = Article::all();
        return view('admin.article.index',compact('data'));
    }

    //添加文章 显示
    public function create()
    {
        return view('admin.article.create');
    }

    //图片上传
    public function upfile(Request $request){
        // 封面图片
        $pic = config('up.pic');
        if ($request->hasFile('file')) {
            // 上传
            // 参数2 配置的节点名称
            $ret = $request->file('file')->store('', 'article');
            $pic = '/uploads/article/' . $ret;
        }
        return ['status' => 0, 'url' => $pic];
    }

    //添加文章 处理
    public function store(Request $request)
    {
        //表单验证
        $this->validate($request,[
            'title' => 'required'
        ]);
        $post = $request->except(['_token','file']);
        //入库
        Article::create($post);
        return ['status'=>0,'msg'=>'添加文章成功'];
    }

    //
    public function show(Article $article)
    {

    }

    //修改文章 显示
    public function edit(Article $article)
    {
        //
        return view('admin.article.edit',compact('article'));
    }

    //修改文章 处理
    public function update(Request $request, Article $article)
    {
        $this->validate($request,[
            'title' => 'required'
        ]);
        $post = $request->except('_token');
        $article->update($post);
        return ['status'=>0,'msg'=>'修改成功'];

    }

    //删除文章
    public function destroy(Article $article)
    {
        $article->delete();
        return ['status' => 0,'msg'=> '删除成功'];
    }
}
